# -*- coding: utf-8 -*-
"""
@Time    : 2024/9/10 20:04 
@Author  : ZhangShenao 
@File    : 3.集合间运算.py 
@Desc    : 集合间运算

Python中的集合与数学概念上的集合类似,可以在多个集合间进行交集、并集、差集等操作

自定义运算符:
Python中允许通过一些特殊的方法来为某种类型或数据结构自定义运算符
如：__add__, __sub__, __mul__ 等等
集合就是通过自定义运算符,重载了&、|等操作符,实现集合之间的运算
"""

s1 = {1, 2, 3, 4, 5}
s2 = {3, 4, 5, 6, 7}

# 求交集
print(f"s1 & s1: {s1 & s2}")  # s1 & s1: {3, 4, 5}
print(f"s1.intersection(s2): {s1.intersection(s2)}")  # s1.intersection(s2): {3, 4, 5}

# 求并集
print(f"s1 | s2: {s1 | s2}")  # s1 | s2: {1, 2, 3, 4, 5, 6, 7}
print(f"s1.union(s2): {s1.union(s2)}")  # s1.union(s2): {1, 2, 3, 4, 5, 6, 7}

# 求差集
print(f"s1 - s2: {s1 - s2}")  # s1 - s2: {1, 2}
print(f"s1.difference(s2): {s1.difference(s2)}")  # s1.difference(s2): {1, 2}

# 求对称差集
# 对称差集=并集-交集
print(f"s1 ^ s2: {s1 ^ s2}")  # s1 ^ s2: {1, 2, 6, 7}
print(f"s1.symmetric_difference(s2): {s1.symmetric_difference(s2)}")

# 判断子集
s3 = {1, 2, 3}
print(f"s3 <= s1: {s3 <= s1}")  # s3 <= s1: True
print(f"s3.issubset(s1): {s3.issubset(s1)}")  # s3.issubset(s1): True

# 判断超集
print(f"s1 >= s3: {s1 >= s3}")  # s1 >= s3: True
print(f"s1.issuperset(s3): {s1.issuperset(s3)}")  # s1.issuperset(s3): True
